package com.xdcplus.web.service.qa.impl;

import cn.hutool.core.util.ObjectUtil;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.redis.RedisUtils;
import com.xdcplus.web.common.pojo.entity.qa.QaMtrlBatchReportPowerDistribute;
import com.xdcplus.web.common.pojo.vo.qa.IvPerformanceVO;
import com.xdcplus.web.common.pojo.vo.qa.QaMtrlBatchReportPowerDistributeVO;
import com.xdcplus.web.mapper.qa.QaMtrlBatchReportPowerDistributeMapper;
import com.xdcplus.web.service.qa.QaMtrlBatchReportPowerDistributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 质量结批报告-分档成品统计(QaMtrlBatchReportPowerDistribute)表服务实现类
 *
 * @author Devin.Sang
 * @since 2024-01-24 11:44:45
 */
@Slf4j
@Service("qaMtrlBatchReportPowerDistributeService")
public class QaMtrlBatchReportPowerDistributeServiceImpl extends BaseServiceImpl<QaMtrlBatchReportPowerDistributeMapper, QaMtrlBatchReportPowerDistribute, QaMtrlBatchReportPowerDistribute, QaMtrlBatchReportPowerDistributeVO> implements QaMtrlBatchReportPowerDistributeService {

    private static final String DISTRIBUTE_KEY = "QA_MTRL_DISTRIBUTE";

    @Autowired
    protected QaMtrlBatchReportPowerDistributeMapper qaMtrlBatchReportPowerDistributeMapper;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public List<QaMtrlBatchReportPowerDistribute> queryReportDetail(String reportCode,
                                                                    Timestamp startTime,
                                                                    Timestamp endTime,
                                                                    String proSpec,
                                                                    double theoryPrdPercent) {

        /// 缓存
        String key = DISTRIBUTE_KEY + reportCode;
        List<Object> qaMtrlCache = redisUtils.lRange(key, 0, redisUtils.lLen(key));
        List<QaMtrlBatchReportPowerDistribute> qaMtrlBatchReportPowerDistributes = qaMtrlCache.stream().map(
                V -> (QaMtrlBatchReportPowerDistribute) V
        ).collect(Collectors.toList());

        if (ObjectUtil.isEmpty(qaMtrlBatchReportPowerDistributes)) {
            qaMtrlBatchReportPowerDistributes =
                    baseMapper.queryPowerDetail(reportCode,
                            startTime.toLocalDateTime()
                                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")),
                            endTime.toLocalDateTime()
                                    .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")), proSpec);
            redisUtils.delete(key);
            for (QaMtrlBatchReportPowerDistribute qaMtrlBatchReportPowerDistribute : qaMtrlBatchReportPowerDistributes) {
                qaMtrlBatchReportPowerDistribute.setProdNum(
                        BigDecimal.valueOf(
                                        qaMtrlBatchReportPowerDistribute.getProdNum()
                                ).multiply(BigDecimal.valueOf(theoryPrdPercent))
                                .setScale(0,BigDecimal.ROUND_FLOOR)
                                .longValue()
                );
                redisUtils.lLeftPush(key,qaMtrlBatchReportPowerDistribute);
            }

        }

        /// 排序
        qaMtrlBatchReportPowerDistributes = qaMtrlBatchReportPowerDistributes.stream().sorted(
                Comparator.comparing(QaMtrlBatchReportPowerDistribute::getCategoryName)
                        .reversed()
        ).collect(Collectors.toList());

        IvPerformanceVO ivPerformanceVO = baseMapper.queryPerformanceDetail(
                reportCode,
                startTime.toLocalDateTime()
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")),
                endTime.toLocalDateTime()
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:00")), proSpec
        );

        if (ivPerformanceVO != null) {

            QaMtrlBatchReportPowerDistribute etaDistribute = new QaMtrlBatchReportPowerDistribute();
            QaMtrlBatchReportPowerDistribute rshuntDistribute = new QaMtrlBatchReportPowerDistribute();
            QaMtrlBatchReportPowerDistribute irevDistribute = new QaMtrlBatchReportPowerDistribute();
            etaDistribute.setReportCode(reportCode);
            rshuntDistribute.setReportCode(reportCode);
            irevDistribute.setReportCode(reportCode);
            etaDistribute.setCategoryName("低效");
            rshuntDistribute.setCategoryName("并阻偏小");
            irevDistribute.setCategoryName("漏电流偏大");

            etaDistribute.setProdNum(
                    BigDecimal.valueOf(
                                    ivPerformanceVO.getEtaNum()
                            ).multiply(BigDecimal.valueOf(theoryPrdPercent))
                            .setScale(0,BigDecimal.ROUND_FLOOR)
                            .longValue()
            );
            rshuntDistribute.setProdNum(
                    BigDecimal.valueOf(
                                    ivPerformanceVO.getRshuntDfDrNum()
                            ).multiply(BigDecimal.valueOf(theoryPrdPercent))
                            .setScale(0,BigDecimal.ROUND_FLOOR)
                            .longValue()
            );
            irevDistribute.setProdNum(
                    BigDecimal.valueOf(
                                    ivPerformanceVO.getIrev2Num()
                            ).multiply(BigDecimal.valueOf(theoryPrdPercent))
                            .setScale(0,BigDecimal.ROUND_FLOOR)
                            .longValue()
            );


            qaMtrlBatchReportPowerDistributes.add(etaDistribute);
            qaMtrlBatchReportPowerDistributes.add(rshuntDistribute);
            qaMtrlBatchReportPowerDistributes.add(irevDistribute);

        }

        return qaMtrlBatchReportPowerDistributes;

    }

    /**
     * 比例计算实际值
     * @param itemNum
     * @param itemTotal
     * @param theoryPrdNum
     * @return
     */
    private Long calculateActualByPercent(Long itemNum, long itemTotal, long theoryPrdNum) {
        return BigDecimal.valueOf(calculateDivValue(itemNum, itemTotal))
                .multiply(BigDecimal.valueOf(theoryPrdNum))
                .setScale(0, BigDecimal.ROUND_FLOOR).longValue();
    }

    private double calculateDivValue(long numerator,
                                     long denominator) {
        return numerator>0 && denominator>0?
                BigDecimal.valueOf(numerator).divide(
                        BigDecimal.valueOf(denominator),8,BigDecimal.ROUND_FLOOR
                ).doubleValue()
                : 0d;
    }


}
